# -*- coding: utf-8 -*-
"""
自定义返回格式
@author:刘飞
@time:2022-01-11
"""
from rest_framework import status
from rest_framework.response import Response
from utils import ecode


# 数据返回规则
def util_response(data='', err=ecode.SUCCESS, http_status=status.HTTP_200_OK, request='', msg=''):
    if http_status == status.HTTP_200_OK:
        if data == [] or data == {}:
            return Response({'err': err, 'msg': msg if msg else ecode.ErrorCode[err], 'data': data})
        elif data:
            return Response({'err': err, 'msg': msg if msg else ecode.ErrorCode[err], 'data': data})
        else:
            return Response({'err': err, 'msg': msg if msg else ecode.ErrorCode[err]})
    else:
        return Response({'err': http_status, 'msg': msg if msg else ecode.ErrorCode.get(http_status, 'error'),
                         'request': request},
                        status=http_status)
